﻿using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using He.BLL;
using System.IO;
using System.Collections.Generic;
using He.Web.UI;
using He.Common;
namespace He.Web.manage.batch
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class add_pic1 : IHttpHandler
    {
        public Model.webset webset = new BLL.webset().GetWebset();
        public void ProcessRequest(HttpContext context)
        {
            //---------------------------------------------------------------------------------------------
            //组件设置a.MD5File为2，3时 的实例代码

            if (context.Request.QueryString["access2008_cmd"] != null && context.Request.QueryString["access2008_cmd"] == "2")//服务器提交MD5验证后的文件信息进行验证
            {
                //  Request.QueryString["access2008_File_name"];    //文件名
                //  Request.QueryString["access2008_File_size"];    //文件大小，单位字节
                //  Request.QueryString["access2008_File_type"];    //文件类型 例如.gif .png
                //  Request.QueryString["access2008_File_md5"];     //文件的MD5签名

                context.Response.Write("0");//返回命令  0 = 开始上传文件， 2 = 不上传文件，前台直接显示上传完成
                context.Response.End();
            }
            else if (context.Request.QueryString["access2008_cmd"] != null && context.Request.QueryString["access2008_cmd"] == "3") //服务器提交文件信息进行验证
            {
                //  Request.QueryString["access2008_File_name"];    //文件名
                //  Request.QueryString["access2008_File_size"];    //文件大小，单位字节
                //  Request.QueryString["access2008_File_type"];    //文件类型 例如.gif .png

                context.Response.Write("1");//返回命令 0 = 开始上传文件,1 = 提交MD5验证后的文件信息进行验证, 2 = 不上传文件，前台直接显示上传完成
                context.Response.End();
            }
            //---------------------------------------------------------------------------------------------

            if (context.Request.Files["Filedata"] != null)//判断是否有文件上传上来
            {
                SaveImages("File/", context);
                //其他表单数据接收

                if (context.Request.QueryString["access2008_File_md5"] != null)
                {
                    context.Response.Write("<br/>");
                    context.Response.Write("MD5效验" + context.Request.QueryString["access2008_File_md5"]);
                }
                context.Response.Write("<br/>");
                context.Response.Write("你选择的是<font color='#ff0000'>" + context.Request.Form["select"] + "</font>--<font color='#0000ff'>" + context.Request.Form["select2"] + "</font>");
                if (context.Request.Form["access2008_box_info_max"] != null)
                {
                    context.Response.Write("<br />组件文件列表统计,总数量：" + context.Request.Form["access2008_box_info_max"] + ",剩余：" + (Convert.ToInt32(context.Request.Form["access2008_box_info_upload"]) - 1) + ",完成：" + (context.Request.Form["access2008_box_info_over"] + 1));
                }
                context.Response.Write(" <br />旋转角度：" + context.Request.Form["access2008_image_rotation"]);
                context.Response.End();
            }
        }


        /// <summary>
        /// 保存文件
        /// </summary>
        /// <param name="url">保存路径,填写相对路径</param>
        /// <returns></returns>
        private void SaveImages(string url, HttpContext context)
        {
            ///'遍历File表单元素
            HttpFileCollection files = HttpContext.Current.Request.Files;

            ///'检查文件扩展名字
            //HttpPostedFile postedFile = files[iFile];
            HttpPostedFile postedFile = context.Request.Files["Filedata"]; //得到要上传文件
            string fileName, fileExtension, filesize;
            fileName = System.IO.Path.GetFileName(postedFile.FileName.ToString()); //得到文件名
            filesize = System.IO.Path.GetFileName(postedFile.ContentLength.ToString()); //得到文件大小
            if (fileName != "")
            {
                Model.article model = new He.Model.article();
                model.category_id = int.Parse(context.Request.Form["ddlcategory_id"]);


                Model.category category = new BLL.category().GetModel(model.category_id);
                Model.channel channel = new BLL.channel().GetModel(category.channel_id);

                fileExtension = fileName.Substring(fileName.LastIndexOf('.') + 1);//'获取扩展名

                //注意：可能要修改你的文件夹的匿名写入权限。
                string fname = FileHelper.MakeFileName(fileExtension);
                string dirName = FileHelper.MakeUploadDire(webset.web_path, channel.name, model.category_id);

                string imgUrl = dirName + fname;
                //保存图片
                postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("~" + imgUrl));
                //是否打图片水印
                if (IsWaterMark(fileExtension.Trim('.')) && webset.watermarktype > 0)
                {
                    switch (this.webset.watermarktype)
                    {
                        case 1:
                            WaterMark.AddImageSignText(imgUrl, imgUrl,
                                this.webset.watermarktext, this.webset.watermarkposition,
                                this.webset.watermarkimgquality, this.webset.watermarkfont, this.webset.watermarkfontsize);
                            break;
                        case 2:
                            WaterMark.AddImageSignPic(imgUrl, imgUrl,
                                this.webset.watermarkpic, this.webset.watermarkposition,
                                this.webset.watermarkimgquality, this.webset.watermarktransparency);
                            break;
                    }
                }

                if (channel.is_thumb == 1)
                {
                    model.img_url = FileHelper.MakeUploadDire(webset.web_path,"thumb/" + channel.name, model.category_id) + FileHelper.MakeFileName(fileExtension);
                    ImageHelper.CompressImage(imgUrl, model.img_url, channel.img_width, channel.img_height, "max");
                }
                else
                {
                    model.img_url = imgUrl;
                }
                model.img_urls = imgUrl;
                int dotIdx = postedFile.FileName.LastIndexOf('.');
                model.title = postedFile.FileName.Substring(0, dotIdx);
                model.price = 0;
                model.content = "";
                model.click = 0;
                model.seo_keywords = "";
                model.seo_desc = "";
                model.is_top = 0;
                model.is_red = 0;
                model.is_hot = 0;
                model.sort_id = 99;
                model.add_time = DateTime.Now.ToString();

                new BLL.article().Add(model);
            }

            context.Response.Write(fileName + "上传成功");


        }
        /// <summary>
        /// 是否需要打水印
        /// </summary>
        /// <param name="_fileExt">文件扩展名，不含“.”</param>
        private bool IsWaterMark(string _fileExt)
        {
            //判断是否开启水印
            if (this.webset.watermarktype > 0)
            {
                //判断是否可以打水印的图片类型
                ArrayList al = new ArrayList();
                al.Add("bmp");
                al.Add("jpeg");
                al.Add("jpg");
                al.Add("png");
                if (al.Contains(_fileExt.ToLower()))
                {
                    return true;
                }
            }
            return false;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
